package com.tanhua.server.service;


import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.server.api.UserLikeApi;

import com.tanhua.server.pojo.User;
import com.tanhua.server.pojo.UserInfo;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.UserInfoPageResult;
import com.tanhua.server.vo.UserInfoVo;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;

import java.util.List;

@Service
public class ManagerService {
    @Autowired
    private UserInfoService userInfoService;
    @Reference(version = "1.0.0")
    private UserLikeApi userLikeApi;

    /**
     * 用户数据翻页
     * @param queryParam
     * @return
     */
    public UserInfoPageResult queryFindUserInfo(RecommendUserQueryParam queryParam) {
        QueryWrapper<UserInfo> wrapper=new QueryWrapper<>();
        if (StringUtils.isNotBlank(queryParam.getId())){
            //需要用户id参数查询
            wrapper.eq("user_id",queryParam.getId());
        }
        if (StringUtils.isNotBlank(queryParam.getNickname())){
            //需要昵称参数查询
            wrapper.like("nick_name",queryParam.getNickname());
        }
        if (StringUtils.isNotBlank(queryParam.getCity())){
            //需要城市参数查询
            wrapper.eq("city",queryParam.getCity());
        }
        IPage<UserInfo> userInfoIPage =userInfoService.queryFindUserInfo(queryParam.getPage(),queryParam.getPagesize(),wrapper);
        UserInfoPageResult pageResult=new UserInfoPageResult();
        pageResult.setPage(queryParam.getPage());
        pageResult.setPagesize(queryParam.getPagesize());
        //获取总条数
        pageResult.setCounts((int) userInfoIPage.getTotal());
        //获取总页数
        pageResult.setPages((int) userInfoIPage.getPages());
        List<UserInfoVo> list=new ArrayList<>();
        List<UserInfo> userInfoList = userInfoIPage.getRecords();
        for (UserInfo userInfo : userInfoList) {
            //封装数据
            UserInfoVo userInfoVo=new UserInfoVo();
            User user = userInfoService.queryUserById(Long.valueOf(userInfo.getUserId()));
            userInfoVo.setId((int)(userInfo.getUserId().longValue()));
            userInfoVo.setLogo(userInfo.getLogo());
            userInfoVo.setLogoStatus(userInfo.getLogoStatus());
            userInfoVo.setNickname(userInfo.getNickName());
            userInfoVo.setMobile(user.getMobile());
            userInfoVo.setSex(userInfo.getSex().getValue()==1? "男":"女");
            userInfoVo.setAge(userInfo.getAge());
            userInfoVo.setOccupation(userInfo.getOccupation());
            userInfoVo.setUserStatus(user.getFreezingRange()==0?"1":"2");
            //todo
            userInfoVo.setLastActiveTime((int) user.getCreated().getTime());
            userInfoVo.setCity(userInfo.getCity());
            list.add(userInfoVo);
        }
        pageResult.setItems(list);
        return pageResult;
    }

    /**
     * 用户基本信息
     * @param userId
     * @return
     */
    public UserInfoVo queryUserById(Integer userId) {
        UserInfoVo userInfoVo=new UserInfoVo();
        String[] arr = {"合肥肥东", "南京", "上海", "深圳", "合肥肥西", "杭州", "武汉","北京"};
        String lastLoginLocation = arr[RandomUtils.nextInt(0, 8)];


        UserInfo userInfo=userInfoService.queryUserInfoById(userId);
        User user=userInfoService.queryUserById(Long.valueOf(userId));
        int likeCount = Math.toIntExact(userLikeApi.queryLikeCount(userId.longValue()));//喜欢数
        int beLikedCount = Math.toIntExact(userLikeApi.queryFanCount(userId.longValue())); //被喜欢数
        int eachLikeCount = Math.toIntExact(userLikeApi.queryEachLikeCount(userId.longValue())); //配对数
        userInfoVo.setId(userId);
        userInfoVo.setAge(userInfo.getAge());
        userInfoVo.setNickname(userInfo.getNickName());
        userInfoVo.setLogo(userInfo.getLogo());
        userInfoVo.setMobile(user.getMobile());
        userInfoVo.setSex(userInfo.getSex().getValue()==1? "男":"女");
        userInfoVo.setUserStatus(user.getFreezingRange()==0?"1":"2");
        userInfoVo.setOccupation(userInfo.getOccupation());
        userInfoVo.setPersonalSignature(userInfo.getPersonalSignature());
        userInfoVo.setIncome(userInfo.getIncome());
        userInfoVo.setCreated((userInfo.getCreated().getTime()));
        userInfoVo.setCity(userInfo.getCity());
        userInfoVo.setCountBeLiked(beLikedCount);
        userInfoVo.setCountLiked(likeCount);
        userInfoVo.setCountMatching(eachLikeCount);
        userInfoVo.setLastActiveTime((int) user.getCreated().getTime());
        userInfoVo.setLastLoginLocation(lastLoginLocation);
        userInfoVo.setLogo(userInfo.getLogo());
        userInfoVo.setTags(userInfo.getTags());
        return userInfoVo;
    }
}


